<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script src="../js/jquery.min.js"></script>
    <script src="../js/axios-0.18.0.js"></script>

</head>
<body>
<div id="app">

    用户名：
    <el-input v-model="pageUser.username" placeholder="根据用户名搜索" style="width: 200px;"></el-input>

    性别：
    <el-input v-model="pageUser.gender" placeholder="根据性别搜索" style="width: 200px;"></el-input>

    邮箱：
    <el-input v-model="pageUser.email" placeholder="根据邮箱搜索" style="width: 200px;"></el-input>

    <el-button @click="find">查询</el-button>
    <el-button @click="add">添加</el-button>

    <el-table :data="tableData" stripe>
        <el-table-column prop="id" label="序号"></el-table-column>
        <el-table-column prop="username" label="用户名"></el-table-column>
        <el-table-column prop="gender" label="性别"></el-table-column>
        <el-table-column prop="password" label="密码"></el-table-column>
        <el-table-column prop="email" label="邮箱"></el-table-column>
        <el-table-column label="操作" align="center">
            <!--
            slot-scope：作用域插槽，可以获取表格数据
            scope：代表表格数据，可以通过scope.row来获取表格当前行数据，scope不是固定写法
            -->
            <template slot-scope="scope">
                <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑
                </el-button>
                <el-button type="danger" size="mini" @click="handleDelete(scope.row)">删除
                </el-button>
            </template>
        </el-table-column>
    </el-table>
    <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pagination.pageCurrent"
            :page-size="pagination.pageSize"
            :page-sizes="[5, 10, 20, 50]"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pagination.total"
            style="float: right">
    </el-pagination>

    <el-dialog
            title="添加用户"
            :visible.sync="dialogVisible"
            width="50%"
            :before-close="handleClose">

        <el-form :model="formData">
            <el-row>
                <el-col>
                    <el-form-item label="序号">
                        <el-input v-model="formData.id"/>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col>
                    <el-form-item label="用户名">
                        <el-input v-model="formData.username"/>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col>
                    <el-form-item label="性别">
                        <el-input v-model="formData.gender"/>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col>
                    <el-form-item label="密码">
                        <el-input v-model="formData.password"/>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col>
                    <el-form-item label="邮箱">
                        <el-input v-model="formData.email"/>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <span slot="footer" class="dialog-footer">
    <el-button @click="cancel">取消</el-button>
    <el-button type="primary" @click="handleAdd">确定</el-button>
  </span>
    </el-dialog>
</div>

<script>
    var vue = new Vue({
        el: '#app',
        data: {
            formData: {},
            dialogVisible: false,
            tableData: [],
            pagination: {
                pageCurrent: 1,
                pageSize: 5,
                total: 0
            },
            pageUser: {
                "username": "",
                "gender": "",
                "email": ""
            },
            addDialogFormVisible: false
        },
        created() {
            this.find();
        },
        methods: {
            handleUpdate(row) {
                this.$message.success("你点击了更新按钮");
            },
            handleDelete(row) {
                this.$message.error("你点击了删除按钮");
            },
            find() {
                //要从后台拿到是否成功的标记，成功/失败
                //要从后台拿到 提示字符串 提示内容 提示用户刚刚的动作是否成功或者失败
                axios.post("/users/" + this.pagination.pageCurrent + "/" + this.pagination.pageSize, this.pageUser).then(res => {
                    // R(Boolean flag,String message,Object data(Long total,Result<User> r))
                    if (res.data.flag === true) {
                        this.$message.success(res.data.message);
                        this.pagination.total = res.data.data.total;
                        this.tableData = res.data.data.r;
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
            },
            handleSizeChange(size) {
                this.pagination.pageSize = size;
                this.find();
            },
            handleCurrentChange(page) {
                this.pagination.pageCurrent = page;
                this.find();
            },
            add() {
                this.formData = {};
                this.dialogVisible = true;
            },
            cancel() {
                this.dialogVisible = false;
            },
            handleAdd() {
                axios.post("/users", this.formData).then(res => {
                    //res(flag,message,data=null)
                    if (res.data.flag == true) {
                        this.$message.success(res.data.message);
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
                this.dialogVisible = false;

            }
        }
    });
</script>
</body>
</html>